package com.jdcjk.project.account.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.project.account.model.RegisteredModel;
import com.jdcjk.project.account.model.entity.Result;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/12/13 0013.
 * 注册程序中介类
 */
public class RegisteredPresenter extends BasePresenter<RegisteredModel> {
    private final String TAG = RegisteredPresenter.class.getSimpleName();

    public RegisteredPresenter(Context context) {
        super(context);
    }

    @Override
    public RegisteredModel bindModel() {
        return new RegisteredModel(getContext());
    }

    /**
     * 注册网络请求
     *
     * @param url                请求地址
     * @param account            账号
     * @param password           密码
     * @param mobCode            验证码
     * @param username           名称
     * @param email              邮箱
     * @param address            地址
     * @param onUIThreadListener 主线程回调
     */
    public void registered(String url, String account, String password, String mobCode, String username, String email, String address, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().registered(url, account, password, mobCode, username, email, address, new StringCallback() {
            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result registerResult = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(registerResult);
            }
        });
    }

    /**
     * 重置密码请求
     *
     * @param url                重置密码URL
     * @param account            账号（手机号）
     * @param mobCode            验证码
     * @param passward           重置密码
     * @param onUIThreadListener 回调
     */
    public void resetPwd(String url, String account, String mobCode, String passward, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().resetPwd(url, account, mobCode, passward, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {

            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result resetPwdResult = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(resetPwdResult);
            }
        });
    }

    /**
     * 获得验证码网络请求
     *
     * @param url                获得验证码URL
     * @param phone              手机号码
     * @param codeType           验证码类型 （注册：SMS_7395039，修改密码：SMS_7395037）
     * @param onUIThreadListener 回调
     */
    public void getVerificationCode(String url, String phone, String codeType, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().getVerificationCode(url, phone, codeType, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result getCodeResult = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(getCodeResult);
            }
        });
    }
}
